package com.note.chh.notebook.UI.enjoy;

import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.note.chh.notebook.Adapter.VideoColletionAdapter;
import com.note.chh.notebook.Model.VideoColletionModel;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.Uitls.gson.GsonImpl;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import butterknife.BindView;
import chuangyuan.ycj.videolibrary.video.VideoPlayerManager;

//视频集
public class VideoColletionActivity extends BaseActivity {


    @BindView(R.id.mRecycle_videoColletion)
    RecyclerView mRecycleVideoColletion;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private int pagerIndex = 0;//pagerIndex==0随机返回一页数据   >=1返回该页码的数据
    private BaseQuickAdapter videoColletionAdapter;
    private LinearLayoutManager layoutManager;
    @Override
    protected int bindLayout() {
        return R.layout.activity_video_colletion;
    }

    @Override
    public void initTopBar() {
        super.initTopBar();
        topbar.setBackgroundColor(getResources().getColor(R.color.app_color_blue));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        topbar.setTitle(getResources().getString(R.string.videoColletion));
    }

    @Override
    public void initView() {
        layoutManager = new LinearLayoutManager(this);
        mRecycleVideoColletion.setLayoutManager(layoutManager);
        mRecycleVideoColletion.setHasFixedSize(true);
        videoColletionAdapter = new VideoColletionAdapter(R.layout.item_video_colletion);
        videoColletionAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRecycleVideoColletion.setAdapter(videoColletionAdapter);
        //设置自动加载
        videoColletionAdapter.setEnableLoadMore(true);

        refreshLayout.autoRefresh();//第一次进入触发自动刷新
        solveRefreshProblem();
    }

    //解决刷新问题
    private void solveRefreshProblem(){
        mRecycleVideoColletion.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                int position = layoutManager.findFirstCompletelyVisibleItemPosition();
                if (position==0) {
                    refreshLayout.setEnabled(true);
                }
            }
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
        mRecycleVideoColletion.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action){
                    case MotionEvent.ACTION_DOWN://0
                        break;
                    case MotionEvent.ACTION_MOVE://2
                        //获取第一个完全显示的条目索引 positions
                        int position = layoutManager.findFirstCompletelyVisibleItemPosition();
                        //达到这个条件就说明滑到了顶部
                        if(position==0){
                            refreshLayout.setEnabled(true);
                        }else{
                            refreshLayout.setEnabled(false);
                        }
                        break;
                    case MotionEvent.ACTION_UP://1
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void initData() {
        /*getVideoData();*/
    }

    @Override
    public void initListener() {
        super.initListener();
        videoColletionAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                pagerIndex++;
                getVideoData();
            }
        }, mRecycleVideoColletion);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pagerIndex = 0;
                getVideoData();
            }
        });
    }

    //获取视频数据
    private void getVideoData() {

        OkGo.<String>get("https://www.apiopen.top/satinApi?type=4&page=" + pagerIndex)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        VideoColletionModel model = GsonImpl.get().toObject(response.body(), VideoColletionModel.class);
                        List<VideoColletionModel.DataBean> data = model.getData();
                        if (pagerIndex == 0) {//随机返回一页数据
                            videoColletionAdapter.setNewData(data);
                            refreshLayout.finishRefresh();
                        } else {
                            if (data.size() >= 20) {
                                videoColletionAdapter.addData(data);
                                videoColletionAdapter.loadMoreComplete();//加载完成
                            } else {
                                videoColletionAdapter.loadMoreEnd();//加载结束
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (pagerIndex == 0) {
                            refreshLayout.finishRefresh();
                        }else{
                            videoColletionAdapter.loadMoreFail();//加载失败
                        }
                    }
                });
    }


    protected void onPause() {
        super.onPause();
        //如果进入详情播放则不暂停视频释放资源
        //为空内部已经处理
        VideoPlayerManager.getInstance().onPause(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        VideoPlayerManager.getInstance().onResume();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        //横竖屏切换
        VideoPlayerManager.getInstance().onConfigurationChanged(newConfig);
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        VideoPlayerManager.getInstance().onDestroy();
    }

    @Override
    public void onBackPressed() {
        //返回监听类
        if (VideoPlayerManager.getInstance().onBackPressed()) {
            finish();
        }
    }

}
